package com.roc.steelend.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.controller.vo.specialInvoice.SpecialInvoiceByKaipingReception;
import com.roc.steelend.controller.vo.specialPay.SpecialByKaipingReception;
import com.roc.steelend.pojo.KaipingGuy;
import com.roc.steelend.service.KaipingGuyService;
import com.roc.steelend.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@RestController
@Api(tags = "开平厂家功能接口")
@CrossOrigin
public class KaipingGuyController {

    @Autowired
    private KaipingGuyService kaipingGuyService;


    @GetMapping("/v1/kaipingguy/pop/show")
    @ApiOperation(value = "进行分页展示开平厂家数据(可带条件)",notes = "可以传关键字进行模糊查询")
    public ReturnMsgForTable kaipingGuyShow(@RequestParam(required = false,value = "searchMsg")@ApiParam(value = "搜索关键字") String searchMsg,
                                            @RequestParam(required = false,value = "now")@ApiParam(value = "当前展示到的页数") Integer now,
                                            @RequestParam(required = false,value = "size")@ApiParam(value = "一次展示多少数据")Integer size){



        if (Objects.equals(searchMsg, "")){
            searchMsg=null;
        }
        List<KaipingGuy> kaipingGuyList = kaipingGuyService.findKaipingGuyList(searchMsg, now, size);
        ReturnMsgForTable msg2 = new ReturnMsgForTable();
        msg2.setObjectList(kaipingGuyList);
        msg2.setCurrenPage(now);
        msg2.setTotal(kaipingGuyService.countKaipingGuy(searchMsg));
        return msg2;
    }



    @GetMapping("/v1/get/kaipingguy/data")
    public KaipingGuy getKaipingGuyData(@RequestParam Integer kaipingGuyId){


        return kaipingGuyService.getKaipingGuyData(kaipingGuyId);
    }

    @PostMapping("/v1/post/account/kaipingguy")
    @ApiOperation(value = "开平厂家界面进行收款接口")
    private ReturnMsg postAccountKaiingGuy(@RequestBody(required = true) SpecialByKaipingReception specialByKaipingReception){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return kaipingGuyService.postAccountKaiingGuy(specialByKaipingReception,Integer.valueOf(employeeIdAsString));
    }

    @PostMapping("/v1/post/invoice/kaipingguy")
    @ApiOperation(value = "开平厂家界面进行开票接口")
    private ReturnMsg postInvoiceKaiingGuy(@RequestBody(required = true) SpecialInvoiceByKaipingReception specialInvoiceByKaipingReception){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return kaipingGuyService.postInvoiceKaiingGuy(specialInvoiceByKaipingReception,Integer.valueOf(employeeIdAsString));
    }
}
